package com.mobi.cacti.ns.view;

import java.util.ArrayList;

import org.holoeverywhere.LayoutInflater;
import org.holoeverywhere.app.Fragment;
import org.holoeverywhere.widget.ListView;

import com.mobi.cacti.R;
import com.mobi.cacti.ns.model.ListItem;
import com.mobi.cacti.ns.view.adapter.ItemListAdapter;
import com.mobi.cacti.rabbit.response.Cache;
import com.mobi.cacti.rabbit.response.ReqMobileLogin;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.FragmentTransaction;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Toast;

public class ContactPersonActivity extends Fragment {

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View v = LayoutInflater.from(getActivity()).inflate(
				R.layout.contactperson_layout, null);
		
		ReqMobileLogin reqMobileLogin = Cache.reqMobileLogin;
		
		if (reqMobileLogin != null && reqMobileLogin.ContactNumber != null && !reqMobileLogin.ContactNumber.isEmpty()) {
		
			ArrayList<ListItem> items = new ArrayList<ListItem>();
	
			ListItem callItem = new ListItem();
			callItem.label = "Corporate Call Center: " + reqMobileLogin.ContactNumber;
			callItem.ticketCountLabel = null;
			callItem.isShowRightArrow = false;
			items.add(callItem);
			
			ListItem mailItem = new ListItem();
			mailItem.label = "Email support: " + reqMobileLogin.ContactEmail;
			mailItem.ticketCountLabel = null;
			mailItem.isShowRightArrow = false;
			items.add(mailItem);
			
			ItemListAdapter adapter = new ItemListAdapter(getSupportActivity(), R.layout.rowitem_layout, items);
			ListView listView = (ListView) v.findViewById(R.id.contactpersonlist);
			listView.setAdapter(adapter);
			
			listView.setOnItemClickListener(new OnItemClickListener() {
				@Override
				public void onItemClick(AdapterView<?> parent, View view, int position,
						long id) {
					
					if(position == 0)
					{
						Uri telUri = Uri.parse("tel:"+Cache.reqMobileLogin.ContactNumber);
						
						Intent callIntent = new Intent(Intent.ACTION_CALL);
						callIntent.setData(telUri);
						startActivity(callIntent);
					}
					else if (position == 1)
					{
						Intent i = new Intent(Intent.ACTION_SEND);
						i.setType("message/rfc822");
						i.putExtra(Intent.EXTRA_EMAIL  , new String[]{Cache.reqMobileLogin.ContactEmail});
						i.putExtra(Intent.EXTRA_SUBJECT, "");
						i.putExtra(Intent.EXTRA_TEXT   , "");
						try {
						    startActivity(Intent.createChooser(i, "Send mail..."));
						} catch (android.content.ActivityNotFoundException ex) {
						    Toast.makeText(view.getContext(), "There are no email clients installed.", Toast.LENGTH_SHORT).show();
						}
					}
				}
			});
		}

		return v;
	}
}
